package com.cpi.cornpp.futurepurchase.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.customerpurchase.service.CustomerPurchaseService;
import com.cpi.cornpp.futurepurchase.exception.FuturePurchaseException;
import com.cpi.cornpp.futurepurchase.form.SearchFuturePurchaseForm;
import com.cpi.cornpp.futurepurchase.services.FuturePurchaseService;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseSearchCriteriaView;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseView;
import com.cpi.cornpp.period.service.PeriodService;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;

public class SearchFuturePurchaseQueryAction extends Action {
	
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String FAILURE_FORWARD = "toFailure";
	
	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws FuturePurchaseException{
		int currentYear = 0;
		int beginPeriodYear = 0;
		int endPeriodYear = 0;
		int orderField = 0;
		int orderType = 0;
		int topForwardYears = 0;
		SearchFuturePurchaseForm searchFuturePurchaseForm = null;
		SystemParameterBean systemParameterBean = null;
		FuturePurchaseService futurePurchaseService = null;
		FuturePurchaseSearchCriteriaView futurePurchaseSearchCriteriaView = null;
		CustomerPurchaseService customerPurchaseService = null;		
		PeriodService periodService = null;
		List periodYears = null;
		List futurePurchases = null;
		List customerPurchases = null;
		ActionForward forward = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		List periods = null;
		
		forward = mapping.findForward(SUCCESS_FORWARD);
		
		if (form != null){
			messageResources = getResources(request);
			periodYears = new ArrayList(0);
			futurePurchaseService = new FuturePurchaseService();
			customerPurchaseService = new CustomerPurchaseService();			
			periodService = new PeriodService();
			searchFuturePurchaseForm = (SearchFuturePurchaseForm) form;
			futurePurchaseSearchCriteriaView = searchFuturePurchaseForm.getFpsView();
			httpSession = request.getSession(false);
			
			try{
				periods = periodService.getActivePeriods();	
				currentYear = Calendar.getInstance().get(Calendar.YEAR);
				beginPeriodYear = searchFuturePurchaseForm.getFpsView().getBeginPeriodYear();
				endPeriodYear = searchFuturePurchaseForm.getFpsView().getEndPeriodYear();
				if( searchFuturePurchaseForm.getOrderField() != 0 ){
					orderField = searchFuturePurchaseForm.getOrderField();
				}
				if( searchFuturePurchaseForm.getOrderType() > 0 ){
					orderType = searchFuturePurchaseForm.getOrderType();
				}
				systemParameterBean = SystemParameterCollection.getInstance().getSystemParameterBean("TOP_FORWARD_YEARS");
				topForwardYears = systemParameterBean.getIntValue();
				
				futurePurchases = futurePurchaseService.getFuturePurchasesByPeriodYearRange(futurePurchaseSearchCriteriaView, orderField, (1 == orderType) ? false : true);
				
				if (!CollectionUtils.isEmpty(futurePurchases)){
					Iterator iterator = futurePurchases.iterator();
					FuturePurchaseView futurePurchaseView = null;
					while(iterator.hasNext()){
						futurePurchaseView = (FuturePurchaseView)iterator.next();
						customerPurchases = customerPurchaseService.getCustomerPurchasesByFuturePurchase(futurePurchaseView.getFuturePurchaseId());
						
						if (!CollectionUtils.isEmpty(customerPurchases)){
							futurePurchaseView.setCustomerPurchases(customerPurchases);
						}
					}
				}
				
				for (int x = 0; x < (topForwardYears * 2); x++){
					periodYears.add((x < topForwardYears) ? Integer.toString((currentYear - topForwardYears + x)) : Integer.toString((currentYear - topForwardYears + x)));
				}
				
				request.setAttribute("periods", periods);
				request.setAttribute("bPeriodYear", new Integer( beginPeriodYear ));
				request.setAttribute("ePeriodYear", new Integer( endPeriodYear ));
				request.setAttribute("periodYears", periodYears);
				request.setAttribute("futurePurchases", futurePurchases);
				
			}catch(FuturePurchaseException futurePurchaseException){
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), futurePurchaseException.getErrorCode());
				if(null == messageError){
					form.reset(mapping, request);
					throw futurePurchaseException;
				}else{
					httpSession = request.getSession(false);
					httpSession.setAttribute(ERROR_MESSAGE, messageError);
					forward = mapping.findForward(FAILURE_FORWARD);
				}
			}catch(Exception exception){
				FuturePurchaseException futurePurchaseException = null;
				exception.printStackTrace(System.out);
				futurePurchaseException = new FuturePurchaseException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
				form.reset(mapping, request);
				throw futurePurchaseException;
			}
		}
		httpSession.setAttribute("previousOrderField", Integer.toString(orderField));
		httpSession.setAttribute("previousOrderType", Integer.toString(orderType));
		return forward;
	}
}